package io.github.uicdb.flame_driven.data;

import com.mojang.datafixers.util.Pair;
import io.github.noeppi_noeppi.libx.annotation.data.Datagen;
import io.github.noeppi_noeppi.libx.crafting.IngredientStack;
import io.github.noeppi_noeppi.libx.mod.ModX;
import io.github.uicdb.flame_driven.ModUtil;
import io.github.uicdb.flame_driven.common.ModRegisteries;
import io.github.uicdb.flame_driven.common.recipe.SingleInputRecipe;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.HashCache;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.block.Blocks;
import net.minecraftforge.common.Tags;
import net.minecraftforge.common.data.ExistingFileHelper;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Datagen
public final class ModRecipesGenerator extends ModGeneratorBase{
    public ModRecipesGenerator(ModX mod, DataGenerator generator, ExistingFileHelper fileHelper) {
        super(mod, generator, fileHelper);
    }

    @Override
    public void run(@NotNull HashCache pCache) {
        generator();
        singleInputRecipeList.forEach((resourceLocation, singleInputRecipe) -> {
            saveData(SingleInputRecipe.Serializer.INSTANCE.toJson(singleInputRecipe),"recipes/single_input/",resourceLocation.getPath(),pCache);
        });
    }

    private void generator() {
        putSingleRecipe(new SingleInputRecipeBuilder("test",100,Ingredient.of(ModRegisteries.kinding))
                .pair(Items.STICK,1)
                .pair(Items.DIRT,1)
                .pair(Items.STRING,1)
                .build()
        );
        putSingleRecipe(new SingleInputRecipeBuilder("test2",100,Ingredient.of(ModRegisteries.kinding),2)
                .pair(Items.DIAMOND,1)
                .build()
        );
    }
    private static class SingleInputRecipeBuilder{
        List<Pair<ItemStack,Double>> pairList=new ArrayList<>();
        final ResourceLocation id;
        final int craftTimes;
        final Ingredient input;
        final int inputSize;
        public SingleInputRecipeBuilder(String id,int craftTimes,Ingredient input,int inputSize){
            this.id=ModUtil.modLoc(id);
            this.craftTimes=craftTimes;
            this.input=input;
            this.inputSize=inputSize;
        }
        public SingleInputRecipeBuilder(String id,int craftTimes,Ingredient input){
            this(id,craftTimes,input,1);
        }
        public SingleInputRecipeBuilder pair(ItemStack itemStack,double weight){
            pairList.add(new Pair<>(itemStack,weight));
            return this;
        }
        public SingleInputRecipeBuilder pair(Item item, double weight){
           return pair(item.getDefaultInstance(),weight);
        }
        public SingleInputRecipe build(){
            return new SingleInputRecipe(new IngredientStack(input,inputSize),pairList,id,craftTimes);
        }
    }

    private final Map<ResourceLocation,SingleInputRecipe> singleInputRecipeList=new HashMap<>();
    public void putSingleRecipe(SingleInputRecipe singleInputRecipe){
        singleInputRecipeList.put(singleInputRecipe.getId(),singleInputRecipe);
    }

    @Override
    public String getName() {
        return super.getName();
    }
}
